/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.sml.SEFM.controller;

import org.springframework.web.servlet.mvc.SimpleFormController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.SimpleFormController;
import vn.com.sml.SEFM.domain.Merchant;
import vn.com.sml.SEFM.BUS.IPBlackBUS;
import vn.com.sml.SEFM.BUS.MerchantBUS;
import vn.com.sml.SEFM.domain.Ip;
import vn.com.sml.SEFM.domain.Ipblacklist;
/** 
 *
 * @author Tunp
 */
public class AddIPController extends SimpleFormController implements Controller {
    private IPBlackBUS ipBUS = new IPBlackBUS();
    private MerchantBUS merchantBUS = new MerchantBUS();

    public void setMerchantBUS(MerchantBUS merchantBUS) {
        this.merchantBUS = merchantBUS;
    }
   
    public void setIpBUS(IPBlackBUS ipBUS) {
        this.ipBUS = ipBUS;
    }
    
    public AddIPController() {
        //Initialize controller properties here or 
        //in the Web Application Context

        setCommandClass(Ipblacklist.class);
        setCommandName("ipaddress");
//        setSuccessView("DetailIP");
        setFormView("AddIP");
    }
    
// @Override
//	public ModelAndView handleRequest(HttpServletRequest arg0,
//			HttpServletResponse arg1) throws Exception {
//    
////        String ip = arg0.getParameter("ip");
//        String merchant = "1";
//        
//        
//            Ipblacklist ipaddress = new Ipblacklist();
//            
//            int id = Integer.parseInt(merchant);
//            Merchant mc = merchantBUS.getDvcntById(id);
//            
//            ipaddress.setMerchant(mc);
////            ipaddress.setIp(ip);
//            ipBUS.saveIpBlack(ipaddress);
//            
//            ModelAndView mv = new ModelAndView("redirectIpblacklist");
//            return mv;
//        
// }
 
 @Override
    protected ModelAndView onSubmit(
    HttpServletRequest request, 
    HttpServletResponse response, 
    Object command, 
    BindException errors) throws Exception {
        ModelAndView mv = new ModelAndView("redirectIpblack");
        //Do something...

        // bat merchantId cua user tai session hien tai
        String merchantId = "1";
        int id = Integer.parseInt(merchantId);
            Merchant mc = merchantBUS.getDvcntById(id);
            
            
        
        // nhan doi tuong The blacklist tu giao dien va luu vao co so du lieu
        Ipblacklist ipaddress = (Ipblacklist) command;
        ipaddress.setMerchant(mc);
        ipBUS.saveIpBlack(ipaddress);
        
        // gui doi tuong vua tao len trang success
        mv.addObject("ipaddress", ipaddress);
        
        return mv;
    }

}
